package com.atguigu.tingshu.album.api;

import com.atguigu.tingshu.album.service.TrackInfoService;
import com.atguigu.tingshu.common.auth.login.annotation.NeedLogin;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.model.album.TrackInfo;
import com.atguigu.tingshu.query.album.TrackInfoQuery;
import com.atguigu.tingshu.vo.album.AlbumTrackListVo;
import com.atguigu.tingshu.vo.album.TrackInfoVo;
import com.atguigu.tingshu.vo.album.TrackListVo;
import com.atguigu.tingshu.vo.album.TrackStatVo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;

@Tag(name = "声音管理")
@RestController
@RequestMapping("api/album/trackInfo")
@SuppressWarnings({"unchecked", "rawtypes"})
public class TrackInfoApiController {

    @Autowired
    private TrackInfoService trackInfoService;

    // Request URL: http://localhost:8500/api/album/trackInfo/findUserTrackPage/1/10

    @Operation(summary = "分页查询用户声音")
    @PostMapping("findUserTrackPage/{pn}/{pz}")
    @NeedLogin
    public Result findUserTrackPage(
            @PathVariable Long pn,
            @PathVariable Long pz,
            @RequestBody TrackInfoQuery trackInfoQuery) {
        Long userId = AuthContextHolder.getUserId();
        trackInfoQuery.setUserId(userId);
        IPage<TrackListVo> page = new Page<>(pn, pz);
        page = trackInfoService.findUserTrackPage(page, trackInfoQuery);
        return Result.ok(page);
    }

    // Request URL: http://localhost:8500/api/album/trackInfo/uploadTrack
    @Operation(summary = "上传声音到vod")
    @PostMapping("uploadTrack")
    public Result uploadTrack(MultipartFile file) {
        Map<String, Object> map = trackInfoService.uploadTrack(file);
        return Result.ok(map);
    }

    // Request URL: http://localhost:8500/api/album/trackInfo/saveTrackInfo
    @Operation(summary = "保存声音信息")
    @PostMapping("saveTrackInfo")
    @NeedLogin
    public Result saveTrackInfo(@RequestBody TrackInfoVo trackInfoVo) {
        trackInfoService.saveTrackInfo(trackInfoVo);
        return Result.ok();
    }

    // Request URL: http://localhost:8500/api/album/trackInfo/getTrackInfo/51951
    @Operation(summary = "根据声音id查询声音信息")
    @GetMapping("getTrackInfo/{trackId}")
    @NeedLogin
    public Result getTrackInfo(@PathVariable Long trackId) {
        TrackInfo trackInfo = trackInfoService.getTrackInfo(trackId);
        return Result.ok(trackInfo);
    }

    // Request URL: http://localhost:8500/api/album/trackInfo/updateTrackInfo/51951
    @Operation(summary = "更新声音信息")
    @PutMapping("updateTrackInfo/{trackId}")
    @NeedLogin
    public Result updateTrackInfo(@PathVariable Long trackId,
                                  @RequestBody TrackInfo trackInfo) {
        Long userId = AuthContextHolder.getUserId();
        trackInfo.setUserId(userId);
        trackInfoService.updateTrackInfo(trackInfo);
        return Result.ok();
    }

    // Request URL: http://localhost:8500/api/album/trackInfo/removeTrackInfo/51952
    @Operation(summary = "删除声音信息")
    @DeleteMapping("removeTrackInfo/{trackId}")
    @NeedLogin
    public Result removeTrackInfo(@PathVariable Long trackId) {
        trackInfoService.removeTrackInfo(trackId);
        return Result.ok();
    }

    // Request URL: http://localhost:8500/api/album/trackInfo/findAlbumTrackPage/1593/1/10
    @Operation(summary = "根据专辑id分页查询专辑下的声音并贴上是否付费的标签")
    @GetMapping("findAlbumTrackPage/{albumId}/{pn}/{pz}")
    @NeedLogin(required = false) // 该接口 可登录 可不登录
    public Result findAlbumTrackPage(@PathVariable Long albumId,
                                     @PathVariable Long pn,
                                     @PathVariable Long pz) {
        IPage<AlbumTrackListVo> albumTrackListVoPage = new Page<>(pn, pz);
        albumTrackListVoPage = trackInfoService.findAlbumTrackPage(albumTrackListVoPage, albumId);
        return Result.ok(albumTrackListVoPage);
    }

    // Request URL: http://localhost:8500/api/album/trackInfo/getTrackStatVo/51953
    @Operation(summary = "根据声音id查询声音的stat")
    @GetMapping("getTrackStatVo/{trackId}")
    public Result getTrackStatVo(@PathVariable Long trackId) {
        TrackStatVo trackStatVo = trackInfoService.getTrackStatVo(trackId);
        return Result.ok(trackStatVo);
    }

    // Request URL: http://localhost:8500/api/album/trackInfo/findUserTrackPaidList/48241
    @Operation(summary = "分集展示用户要购买的声音列表")
    @GetMapping("findUserTrackPaidList/{trackId}")
    @NeedLogin
    public Result findUserTrackPaidList(@PathVariable Long trackId) {
        Long userId = AuthContextHolder.getUserId();
        List<Map<String, Object>> map = trackInfoService.findUserTrackPaidList(trackId, userId);
        return Result.ok(map);
    }

}

